package cn.gyn.gynrpc.core.meta;

import com.alibaba.fastjson.JSON;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.HashMap;
import java.util.Map;

/**
 * 描述RC的实例描述
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class InstanceMeta {

    // [skiːm]"方案"、"计划" 或 "体系" 比如：http
    private String scheme;
    private String host;
    private Integer port;
    //路径的上下文，端口号后可能会有/路径的一些描述
    private String context;
    //实例是否在线的状态 true-online false-offline
    private boolean status;
    //存放机房等标识的参数
    private Map<String, String> parameters = new HashMap<>();

    public InstanceMeta(String scheme, String host, Integer port, String context) {
        this.scheme = scheme;
        this.host = host;
        this.port = port;
        this.context = context;
    }

    public String toPath() {
        //%s表示string，%d表示整数
        return String.format("%s_%d", host, port);
    }

    public static InstanceMeta http(String host, Integer port) {
        return new InstanceMeta("http", host, port, "gynrpc");
    }

    public String toUrl() {
        //拼接一个http请求用的合法url
        return String.format("%s://%s:%d/%s", scheme, host, port, context);
    }

    public String toMetas() {
        return JSON.toJSONString(this.getParameters());
    }

    public InstanceMeta addParams(Map<String, String> params) {
        this.getParameters().putAll(params);
        return this;
    }
}
